package com.netbliz.ftpmanagment.action;

import java.io.IOException;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.json.JSONException;
import org.json.JSONObject;

import com.netbliz.common.action.BaseAction;
import com.netbliz.ftpmanagment.service.LoginService;
import com.netbliz.ftpmanagment.util.Page;
import com.netbliz.ftpmanagment.util.SessionUtil;

@SuppressWarnings("all")
public class LoginAction extends BaseAction {
	private LoginService loginService;
	public LoginService getLoginService() {
		return loginService;
	}
	public void setLoginService(LoginService loginService) {
		this.loginService = loginService;
	}
	/**
	 * 转录登录页面
	 * @return
	 */
	public String toLogin(){
		return Page.TOLOGIN;
	}
	/**
	 * 登录成功页面
	 * @return
	 */
	public String login(){
		String userID=SessionUtil.getSession().get("userID").toString();
		if(userID!=null && !userID.trim().equals("")){
			if(userID.trim().toLowerCase().equals("admin")){
				return Page.MAIN_ADMIN;
			}else{
				return Page.MAIN_DPD;
			}
		}
		return Page.ERROR;
	}
	/**
	 * 验证用户名及密码
	 */
	public void ajaxLogin(){
		JSONObject jRequest=getJsonObjectByRequestData();
		String username=null;
		String password=null;
		if(jRequest!=null){
			try {
				username=jRequest.getString("username");
				password=jRequest.getString("password");
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		boolean result=this.loginService.findUser(username, password);
		JSONObject jResult=new JSONObject();
		try {
			jResult.put("result", result?1:0);
		} catch (JSONException e) {
			e.printStackTrace();
		}
		if(result){
			SessionUtil.getSession().put("userID", username);
		}
		writeJson(jResult.toString());
	}
	/**
	 * 注销
	 * @return
	 */
	public String logout(){
		SessionUtil.sessionClear();
		return Page.LOGOUT;
	}
}
